$(function () {
    var baseUrl=window.location.protocol+"//"+window.location.host;

    layui.use('layer', function() {

        $('input[name=problem]').on('change',function () {
           if($(this).val()==''){
               $(this).css('border','1px solid #f00');
           }else{
               $(this).css('border','1px solid #00C1DE');
           }
        });

        //提问
        $('#ask-btn').on('click',function () {
            if($(this).attr('data-status')==1) return false;

            if($('input[name=problem]').val()==''){$('input[name=problem]').css('border','1px solid #f00'); return false; }

            $('input[name=problem]').css('border','1px solid #00C1DE');

            $(this).attr('data-status',1);

            var that=$(this);

            var data={
                content:$('input[name=problem]').val(),
            };

            $.ajax({
                type: "POST",
                url: baseUrl+"/api/problem/add",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000
                        }, function(){
                            that.attr('data-status',0);
                            window.location.reload();
                        });
                    }else{
                        that.attr('data-status',0);
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });

        });

        //留言管理
        $('#message').on('click',function () {
            if($(this).attr('data-status')==1) return false;

            if($('input[name=mobile]').val()==''){ $('input[name=mobile]').css('border','1px solid #f00'); return false; }
            $('input[name=mobile]').css('border','1px solid #00C1DE');

            if($('input[name=nickname]').val()==''){ $('input[name=nickname]').css('border','1px solid #f00'); return false; }
            $('input[name=nickname]').css('border','1px solid #00C1DE');

            if($('input[name=content]').val()==''){ $('input[name=content]').css('border','1px solid #f00'); return false; }
            $('input[name=content]').css('border','1px solid #00C1DE');

            $(this).attr('data-status',1);

            var that=$(this);

            var data={
                content:$('input[name=content]').val(),
                mobile:$('input[name=mobile]').val(),
                nickname:$('input[name=nickname]').val(),
            };

            $.ajax({
                type: "POST",
                url: baseUrl+"/api/message/add",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000
                        }, function(){
                            that.attr('data-status',0);
                            window.location.reload();
                        });
                    }else{
                        that.attr('data-status',0);
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });

        });


    });

});